<?php

namespace App\Admin\Controllers;

use App\Models\ProductCategory;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Request;
use App\Admin\Actions\Get\AddNextLevel;
use App\Admin\Actions\Get\Destory;
use App\Admin\Actions\Post\Restore;

class ProductCategoryController extends AdminController
{
    protected $title = '商品分类';

    protected function grid()
    {
        $pid = Request::get('pid', 0);
        $grid = new Grid(new ProductCategory());
        $grid->model()->where('pid', $pid);
        $grid->column('id', 'Id号');
        $grid->column('sort', __('排序'));
        $grid->column('name', __('分类名称'))->display(function ($name) {
            return "<a href='" . url('admin/product_category?pid=') . $this->id . "'>$name</a>";
        });
        $grid->column('icon', __('图片'))->image(self::$baseImg, 100, 100);

        $grid->column('is_shortcut', '是否显示在首页?')->display(function ($is_shortcut) {
            return $is_shortcut ? '是' : '否';
        });

        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        $grid->disableExport();
        $grid->disableColumnSelector();

        $grid->actions(function ($actions) {
            $actions->disableDelete();
//            $actions->add(new AddNextLevel());
            $actions->add(new Destory());

        });

        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->like('name', '分类名称');

        });

        return $grid;
    }

    protected function detail($id)
    {
        $show = new Show(ProductCategory::findOrFail($id));

        $show->field('sort', __('排序'));
        $show->field('name', __('分类名称'));
        $show->field('pid', __('父级分类'))->as(function ($pid) {
            if ($pid == 0) {
                $pname = '顶级分类';
            } else {
                $pname = ProductCategory::where('id', $pid)->value('name');
            }
            return $pname;
        });

        $show->field('is_shortcut', '是否显示在首页')->using(['1' => '是', '0' => '否']);

        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });

        return $show;
    }

    protected function form()
    {
        $form = new Form(new ProductCategory());

        $form->number('sort', __('排序'));

        if ($form->isCreating()) {
            $pid = Request::input('pid', 0);
        } elseif ($form->isEditing()) {
            $id = request()->route()->parameters()["product_category"];
            $pid = ProductCategory::where('id', $id)->value('pid');
        };
        if ($pid == 0) {
            $pname = '顶级分类';
        } else {
            $pname = ProductCategory::where('id', $pid)->value('name');
        }

        $options = [
            $pid => $pname,
        ];


        $form->select('pid', '父级分类')->options($options)->default($pid)->rules('required', [
            'required' => '必须选择父级分类',
        ]);
        $form->text('name', __('分类名称'))->rules('required', [
            'required' => '分类名称不能为空',
        ]);

        $form->image('icon', '图片')->required();

        $states = [
            'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        ];

        $form->switch('is_shortcut', '是否显示在首页')->states($states);

        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();

        });

        $form->footer(function ($footer) {
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });

        return $form;
    }
}